package edu.example;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.lang.reflect.Array;
import java.util.Vector;
import java.util.Collection;
import java.util.Iterator;
import java.util.TreeMap;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;



public class UploadAppDialog extends JDialog
{
	//File Choicer
	private JFileChooser fc;
	
	private String ayFileLocation;
	private String otsFileLocation;

	//GUI Component
	private JRadioButton phdButton;
	private JRadioButton msButton;
	
	private JLabel applyYLabel;
	private JLabel otsLabel;
	
	private JTextField applyYField;
	private JTextField otsField;
	
	
	private JButton browseAYButton;
	private JButton browseOTSButton;
	
	private JButton removeButton;
	private JButton removeButton1;
	
	private JButton okButton;
	private JButton cancelButton;
	
	private JPanel radioButtonPanel;
	private JPanel uploadPanel;
	private JPanel okButtonPanel;

	//Handlers
	private RemoveHandler removeHandler;
	private BrowseAYHandler browseAYHandler;
	private BrowseOTSHandler browseOTSHandler;
	private OkHandler okHandler;
	private CancelHandler cancelHandler;
	
	/**
	 * Constructor
	 */
	public UploadAppDialog()
	{
		layoutGUI();
		this.setSize(450, 200);
		this.setResizable(false);
	}
	
	public void layoutGUI()
	{
		//center the frame
		int w = 450;
		int h = 200;
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		int screenWidth = screenSize.width;
		int screenHeight = screenSize.height;
		int x = (screenWidth - w)/2;
		int y = (screenHeight - h)/2;
		setLocation(x,y);	
		
		//Construct GUI components
		fc = new JFileChooser();
		
		msButton = new JRadioButton("M.S.");
		phdButton = new JRadioButton("Ph.D.");
		
		ButtonGroup group = new ButtonGroup();
		group.add(msButton);
		group.add(phdButton);
		msButton.setSelected(true);
		
		applyYLabel = new JLabel("ApplyYourself File: ");
		otsLabel = new JLabel("Offical Test Score File: ");
		
		applyYField = new JTextField(10);
		otsField = new JTextField(10);
		
		browseAYButton = new JButton("Browse");
		browseOTSButton = new JButton("Browse");
		
		removeButton = new JButton("Remove");
		removeButton1 = new JButton("Remove");
		
		okButton = new JButton("OK");	
		cancelButton  = new JButton("Cancel");
		
		radioButtonPanel = new JPanel();
		uploadPanel = new JPanel();
		okButtonPanel = new JPanel();
		
		//Setup layout
		GridBagLayout gbl = new GridBagLayout();
		uploadPanel.setLayout(gbl);
		
		addJComponentToContainerUsingGBL(applyYLabel, 		uploadPanel,	0,	0,	1,	1);
		addJComponentToContainerUsingGBL(applyYField, 		uploadPanel,	1,	0,	1,	1);
		addJComponentToContainerUsingGBL(browseAYButton, 		uploadPanel,	2,	0,	1,	1);
		addJComponentToContainerUsingGBL(removeButton, 		uploadPanel,	3,	0,	1,	1);
		addJComponentToContainerUsingGBL(otsLabel, 			uploadPanel,	0,	1,	1,	1);
		addJComponentToContainerUsingGBL(otsField, 			uploadPanel,	1,	1,	1,	1);
		addJComponentToContainerUsingGBL(browseOTSButton, 	uploadPanel,	2,	1,	1,	1);
		addJComponentToContainerUsingGBL(removeButton1, 	uploadPanel,	3,	1,	1,	1);
		
		radioButtonPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		radioButtonPanel.add(msButton);
		radioButtonPanel.add(phdButton);
		
		okButtonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
		okButtonPanel.add(okButton);
		okButtonPanel.add(cancelButton);
		
		this.add(radioButtonPanel, BorderLayout.NORTH);
		this.add(uploadPanel, BorderLayout.WEST);
		this.add(okButtonPanel, BorderLayout.SOUTH);
		
		//Construct handler
		removeHandler = new RemoveHandler();
		browseAYHandler = new BrowseAYHandler();
		browseOTSHandler = new BrowseOTSHandler();
		okHandler = new OkHandler();
		cancelHandler = new CancelHandler();
		
		//Link component to hander
		okButton.addActionListener(okHandler);
		cancelButton.addActionListener(cancelHandler);
		
		browseAYButton.addActionListener(browseAYHandler);
		browseOTSButton.addActionListener(browseOTSHandler);
		
		removeButton.addActionListener(removeHandler);
		removeButton1.addActionListener(removeHandler);
	}
	
	/**
	 * GridBadLayout helper method
	 */
	public void addJComponentToContainerUsingGBL(	JComponent jc,
			Container c,
			int x,
			int y,
			int w,
			int h)
	{
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = w;
		gbc.gridheight = h;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.insets = new Insets(5, 5, 5, 5);
		c.add(jc, gbc);
	}

	
	class BrowseAYHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent ae)
		{
            int returnVal = fc.showOpenDialog(UploadAppDialog.this);

            if (returnVal == JFileChooser.APPROVE_OPTION) 
            {
                File file = fc.getSelectedFile();
                applyYField.setText(file.getPath());
            }
		}	
	}
	
	class BrowseOTSHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent ae)
		{
            int returnVal = fc.showOpenDialog(UploadAppDialog.this);

            if (returnVal == JFileChooser.APPROVE_OPTION) 
            {
                File file = fc.getSelectedFile();
                otsField.setText(file.getPath());
            }
		}	
	}
	
	class RemoveHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent ae)
		{
			JButton button = (JButton) ae.getSource();
			//Find out which button was clicked
			for(int i = 0; i< uploadPanel.getComponentCount(); i++)
			{
				Component[] a = uploadPanel.getComponents();
				if(a[i] == button)
				{
					JTextField field = (JTextField) a[i-2];
					field.setText("");
				}
			}
		}
	}
	
	
	public static void main(String[] args)
	{
		UploadAppDialog frame = new UploadAppDialog();
		frame.setVisible(true);
	}
	
	class OkHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent ae)
		{
			ayFileLocation = applyYField.getText();
            otsFileLocation = otsField.getText();
			{
				File file1 = null;
				File file3 = null;
				boolean flag = false;
				if(ayFileLocation != null)
					file1 = new File(ayFileLocation);
				File file2 = new File("AY-map.txt");
				if(otsFileLocation != null)
					file3 = new File(otsFileLocation);
				File file4 = new File("OTS-map.txt");
				ApplicationManagerCloud	 formatter = new ApplicationManagerCloud(UploadAppDialog.this,file1, file2,file3,file4);
				
				flag = formatter.getFlag();	
				if(flag)
				{

					if(!formatter.getNewApps().isEmpty())
					{
						AttachFileFrame tmp = new AttachFileFrame(formatter.getNewApps());
						tmp.setVisible(true);
					}
					UploadAppDialog.this.setVisible(false);
				}
			}
		}
	}
	
	class CancelHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent ae)
		{
			UploadAppDialog.this.setVisible(false);
		}
	}
}
